package com.example.demo.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import com.example.demo.util.Mailutil;

@Service
@Transactional
public class UserServiceImpl implements UserService{

	@Autowired
	private UserMapper userMapper;
	
	private List<User> list;
	
	private User user1;

	@Autowired
	private Mailutil mailutil;
	
	@Override
	public int insertUser(User user){
		if(user.getLastlogintime()==null){
			user.setLastlogintime(user.getRegistertime());
		}
		int as= userMapper.insertUser(user);
		if(as==1){
			String content=String.format("<!DocType html><html><head><meta charaset='utf-8'/><title>邮件验证界面</title></head><body>亲爱的:%s,您需要点击此处<a href='http://localhost:8080/getUsers/registerUser?uuid=%s'>这里</a></body></html>进行邮箱验证,谢谢合作", new Object[]{user.getUname(),user.getUuid()});
			mailutil.sendHtmlMail(user.getEmail(),"注册用户的邮箱验证邮件",content);
		}
		return as;
	}
	@Override
	public int deleteUser(Integer uid){
		
		return userMapper.deleteUser(uid);
	}
	@Override
	public int updateUser(User user){
		
		return userMapper.updateUser(user);
	}
	@Override
	public int updatePassword(String password){
		if(password!=null && password.length()==32){
			return userMapper.updatePassword(password);
		}
	    return 0;
	}
	
	@Override
	public int updateName(User user){
		if(user!=null){
			return userMapper.updateName(user);
		}
		return 0;
	}
	@Override
    public int updateUUID(User user){
    	
    	return userMapper.updateUUID(user);
    }
	@Override
	public List<User> selectUser(){
		
		list= new ArrayList<User>();
		list=userMapper.selectUser();
		return list;
	}
	@Override
	public User selectOneUser(Integer uid){
		user1= new User();
		user1 =userMapper.selectOneUser(uid);
		return user1;
	}
	@Override
	public User selectNameAndPassword(User user){
		user1= new User();
		user1=userMapper.selectNameAndPassword(user);
		return user1;
	}
	@Override
	public int selectUid(User user){
		
		return userMapper.selectUid(user);
	}
	@Override
	public User selectUUID(String uuid){
		
		return userMapper.selectUUID(uuid);
	}
	@Override
	public String selectUUIDAndName(String loname){
		String uuid= userMapper.selectUUIDAndName(loname);
		return uuid;
	}
	@Override
	public Integer selectRid(String loname){
		
		return userMapper.selectRid(loname);
	}

}
